<?php
namespace app\index\controller;
use think\View;
// use \think\Config;
use think\Db;
use app\index\model\Login;
use think\Controller;
use think\Request;
use think\Cookie;
class User extends Controller
{

    //登录
    public function login(){
        return $this->fetch();
    }

    //验证登录
    public function checkLogin(){
         $data = \think\Request::instance()->post();
        if(isset($data['username']) && isset($data['password'])){
            $link = Db::connect('db2');
            $islogin = isset($data['keep'])?$data['keep']:"0";
            $result= $link->table('user')
            ->where([
                'username'=>$data['username'],
                'password'=>MD5($data['password'])
                ])
            ->select();
            if($result){
                // //登录成功
                if($islogin==1){
            //记住密码
                     Cookie::set("username",$data["username"],7*24*60*60); 
          }else{
              Cookie::set("username",$data["username"]);
            }
                 $this->success('登录成功，正在跳转','Index/lists','',1);
            }else{
                //登录失败
                $this->error('登录失败用户不存在，请重新登录','login','',1);
            }
    }else{  //如果不是POST提交，跳转到登录页
    $this->error('不是POST提交,请重新登录','login','',1);
}

    }

    //注册
    public function register(){
           return $this->fetch();
    }

    public function registerpost(){
              $data = \think\Request::instance()->post();
              $link = Db::connect('db2');
              $result = $link->table('user')->insert([
                    'username'=>$data['username'],
                    'password'=>MD5($data['password'])
             ]);
             if($data['password']==$data['checkpassword']){
                 $this->success('注册成功，请登录','login','',1);
             }else{
                //注册失败
                $this->error('注册失败，请重新注册','register','',1);
            }
    }
}



?>